import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import { useRoute } from "vue-router";
import { checkedInput } from "@/utils/public/regular"
import { ref, reactive, nextTick, watch, onMounted, computed } from "vue";
import {
  ProRiskGetProRisk,
  proinsureOntouchUnder,
  proinsureGetProInsure
} from "@/api/stu/InsuranceData"
import qs from "qs";
export function usePreInsuranceRiskControl() {
  const route = useRoute();
  const columns = [
    {
      type: "selection",
      align: "center",
      width: 30,
      selectable: function (row, index) {
        return !row.result
      }
    },
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "投保人",
      prop: "piApplicant",
    },
    {
      label: "保险产品名称",
      prop: "ppName",
    },
    {
      label: "保险类型",
      prop: "pType",
    },
    {
      label: "申请时间",
      prop: "applyTime",
    },
    {
      label: "风险等级",
      prop: "riskLv",
    },
    {
      label: "核保结果",
      prop: "result",
    }
  ];
  const dataList = ref([]);
  const loading = ref(false);
  const btnloading = ref<boolean>(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    pageSizes: [10, 30, 50, 100],
    background: true
  });
  const DialogVisible = ref(false);
  const modelList = ref([]);// 风控模型
  const selectmodel = ref();//选择的风控内容
  const checkList = ref([]);
  async function oneclickinsurance() {
    if (!checkList.value.length) {
      ElMessage.warning("请选择产品内容")
      return
    }
    if (!selectmodel.value) {
      ElMessage.warning("请选择风控模型")
      return
    }
    btnloading.value = true;
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
      id: selectmodel.value.id,  //风控模型Id
      name: selectmodel.value.name,   //风控模型名称
      insureTabel: checkList.value.map(item => {
        return {
          id: item.piId,
          name: item.piApplicant,
          insureName: item.ppName,
          pType: item.pType,
          applyTime: item.applyTime
        }
      })
    };
    const res = await proinsureOntouchUnder(data);
    if (res.success) {
      ElMessage.success(res.msg);
      close_from()
      btnloading.value = false;
    } else {
      ElMessage.error(res.msg)
      setTimeout(() => { btnloading.value = false; }, 1500)
    }
  }

  // 选中的数据s
  // 选择的列表数据
  function handleSelectionChange(selection) {
    checkList.value = selection;
  }

  // 获取列表
  async function GetDataList() {
    const data = {
      "pageIndex": pagination.currentPage,
      "pageCount": pagination.pageSize,
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    }
    const res = await proinsureGetProInsure(data);
    console.log("2")
    if (res.success) {
      dataList.value = res?.data?.items;
      pagination.total = res?.data?.totalCount
    } else {
      ElMessage.error(res.msg)
    }
  }

  // 获取模型列表
  async function GetModelDataList() {
    const data = {
      "pageIndex": 1,
      "pageCount": 1000,
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    };
    const res = await ProRiskGetProRisk(data);
    if (res.success) {
      modelList.value = res?.data?.items;
    } else {
      ElMessage.error(res.msg);
    }
  }

  // 关闭弹窗
  function close_from() {
    DialogVisible.value = false;
    GetDataList()
    console.log("1")
    selectmodel.value = "";
    checkList.value = []
  }

  function handleSizeChange() {
    GetDataList()
  }

  function handleCurrentChange() {
    GetDataList()
  }

  onMounted(() => {
    GetDataList()
    GetModelDataList()
  })
  return {
    columns,
    dataList,
    loading,
    modelList,
    btnloading,
    pagination,
    selectmodel,
    DialogVisible,
    close_from,
    handleSizeChange,
    oneclickinsurance,
    handleCurrentChange,
    handleSelectionChange
  }
}
